package com.industrial.governance.controller;


import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.governance.domain.QueryParams;
import com.industrial.governance.vo.BandwidthMonitorVo;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.util.Map;
import java.util.Set;

/**
 * 流量监控
 */
@RestController
@RequestMapping("/data/bandwidthMonitor")
public class BandwidthMonitorController extends BaseController {

    /**
     * 查询数量
     * @return
     */
    @GetMapping("/queryCount")
    public AjaxResult queryCount(){

        OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

        // 获取网络IO信息，不同的JVM实现可能返回不同的值
        // 对于Sun的JVM，可以转换为sun.management.OperatingSystemImpl类型来访问更多方法
        // 如getCommittedVirtualMemorySize()
        com.sun.management.OperatingSystemMXBean sunOsBean = (com.sun.management.OperatingSystemMXBean) osBean;

        // 获取总的上行带宽
        long totalUploadThroughput = sunOsBean.getCommittedVirtualMemorySize() / 1024; // 转换为KB

        // 获取总的下行带宽
        long totalDownloadThroughput = sunOsBean.getFreePhysicalMemorySize() / 1024; // 转换为KB
        BandwidthMonitorVo bandwidthMonitorVo=new BandwidthMonitorVo();
        bandwidthMonitorVo.setUploadThroughput(totalUploadThroughput);
        bandwidthMonitorVo.setDownloadThroughput(totalDownloadThroughput);

        return AjaxResult.success(bandwidthMonitorVo);
    }

}
